package com.ruoyi.web.controller.sampleRecord;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.sampleRecord.domain.SampleRecord;
import com.ruoyi.sampleRecord.service.ISampleRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 领样记录Controller
 *
 * @author ruoyi
 * @date 2025-05-12
 */
@RestController
@RequestMapping("/sampleRecord/sampleRecord")
public class SampleRecordController extends BaseController
{
    @Autowired
    private ISampleRecordService sampleRecordService;

    @Autowired
    private ISysUserService userService;
    /**
     * 查询领样记录列表
     */
    @PreAuthorize("@ss.hasPermi('sampleRecord:sampleRecord:list')")
    @GetMapping("/list")
    public  TableDataInfo list(SampleRecord sampleRecord)
    {
        //权限 项目经理和实验员 看被分配到的     业主和业主单位看自己单位的
        if(sampleRecord.getIsRoleShow() != null && sampleRecord.getIsRoleShow() == 1){
            Long userId = SecurityUtils.getLoginUser().getUserId();
            JSONObject jb = userService.getEngineeringIdsForUserId(userId);
            Long enterpriseId = (Long) jb.get("enterpriseId");
            System.out.println("ddggeeee");
            System.out.println(jb);
            List<Long> engineeringIds = (List<Long>) jb.get("engineeringIds");
            if(enterpriseId != 0){
                sampleRecord.setEnterpriseId(enterpriseId);
            }else {
                //项目经理，实验员，劳务实验员
                if (!engineeringIds.isEmpty()) {
                    sampleRecord.setIdsList(engineeringIds);
                } else {
                    List<Long> null_ids = new ArrayList<>();
                    null_ids.add(-1L);
                }
            }
        }
        startPage();
        List<SampleRecord> list = sampleRecordService.selectSampleRecordList(sampleRecord);
        return getDataTable(list);
    }

    /**
     * 导出领样记录列表
     */
    @PreAuthorize("@ss.hasPermi('sampleRecord:sampleRecord:export')")
    @Log(title = "领样记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SampleRecord sampleRecord)
    {
        List<SampleRecord> list = sampleRecordService.selectSampleRecordList(sampleRecord);
        ExcelUtil<SampleRecord> util = new ExcelUtil<SampleRecord>(SampleRecord.class);
        return util.exportExcel(list, "领样记录数据");
    }

    /**
     * 获取领样记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('sampleRecord:sampleRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sampleRecordService.selectSampleRecordById(id));
    }

    /**
     * 新增领样记录
     */
    @PreAuthorize("@ss.hasPermi('sampleRecord:sampleRecord:add')")
    @Log(title = "领样记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SampleRecord sampleRecord)
    {
        return toAjax(sampleRecordService.insertSampleRecord(sampleRecord));
    }

    /**
     * 修改领样记录
     */
    @PreAuthorize("@ss.hasPermi('sampleRecord:sampleRecord:edit')")
    @Log(title = "领样记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SampleRecord sampleRecord)
    {
        sampleRecord.setUpdateType(DicConstants.BUSINESS_Manager.getCode());
        return toAjax(sampleRecordService.updateSampleRecord(sampleRecord));
    }

    /**
     * 删除领样记录
     */
    @PreAuthorize("@ss.hasPermi('sampleRecord:sampleRecord:remove')")
    @Log(title = "领样记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sampleRecordService.logicDeleteSampleRecord(ids,DicConstants.BUSINESS_Manager.getCode(),SecurityUtils.getUserId()));
    }
}
